package DepthFirstSearch;

public class _733_FloodFill {
    int preColor;
    int newColor;
    int[][] image;
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        if(newColor == image[sr][sc]) return image;
        preColor = image[sr][sc];
        this.image = image;
        this.newColor = newColor;
        modifiedImage(sr, sc);
        return image;
    }

    public void modifiedImage(int sr, int sc) {
        if (sr - 1 >= 0) {
            if (image[sr - 1][sc] == preColor) {
                image[sr - 1][sc] = newColor;
                modifiedImage(sr - 1, sc);
            }
        }
        if (sr + 1 < image.length) {
            if (image[sr + 1][sc] == preColor) {
                image[sr + 1][sc] = newColor;
                modifiedImage(sr + 1, sc);
            }
        }
        if (sc - 1 >= 0) {
            if (image[sr][sc - 1] == preColor) {
                image[sr][sc - 1] = newColor;
                modifiedImage(sr, sc - 1);
            }
        }
        if (sc + 1 < image[0].length) {
            if (image[sr][sc + 1] == preColor) {
                image[sr][sc + 1] = newColor;
                modifiedImage(sr, sc + 1);
            }
        }
        image[sr][sc] = newColor;
    }
}
